// $Id: Customer.java 94 2011-01-21 18:56:20Z E106005 $
package it.reichel.entity;

import com.googlecode.objectify.annotation.Cached;
import com.googlecode.objectify.annotation.Unindexed;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.logging.Logger;

@Entity
@Named
@Cached
public final class Customer implements Serializable{

    @Transient
    private static final Logger log = Logger.getLogger(Customer.class.getName());

    @Transient
	private static final long serialVersionUID = -3938678903897918425L;

    @Id private Long id;

    @NotNull(message = "{customerNameError}")
    @Size(min = 3, max = 20, message = "{customerNameError}")
	private String firstName;

    @NotNull(message = "Sobrenome inválido")
    @Size(min = 3, max = 30, message = "O sobrenome deve ter entre 3 e 30 caracteres")
    private String lastName;

    @NotNull
    @Size(min = 5, max = 30, message = "O email deve ter entre 5 e 30 caracteres")
    @Pattern(regexp = "[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]+", message = "Email invalido")
    private String email;

    @Unindexed
    private String details;

    public String getDetails() {
        return details;
    }

    public void setDetails(String details) {
        this.details = details;
    }
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}

    @Override
    public String toString() {
        return "Customer{" +
                "id=" + id +
                ", firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", email='" + email + '\'' +
                ", details='" + details + '\'' +
                '}';
    }

    @PostConstruct
    public void postConstruct() {
        log.info("Chamou o postConstruct do Customer");
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Customer customer = (Customer) o;

        if (details != null ? !details.equals(customer.details) : customer.details != null) return false;
        if (email != null ? !email.equals(customer.email) : customer.email != null) return false;
        if (firstName != null ? !firstName.equals(customer.firstName) : customer.firstName != null) return false;
        if (id != null ? !id.equals(customer.id) : customer.id != null) return false;
        if (lastName != null ? !lastName.equals(customer.lastName) : customer.lastName != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (firstName != null ? firstName.hashCode() : 0);
        result = 31 * result + (lastName != null ? lastName.hashCode() : 0);
        result = 31 * result + (email != null ? email.hashCode() : 0);
        result = 31 * result + (details != null ? details.hashCode() : 0);
        return result;
    }
}
